package confidence.helpers;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;

public class Status {
	public static boolean [] tagsActive = new boolean [14]; // Active tags
	private static Properties settings;
	public static String SETTINGS_FILE_NAME = "settings.conf";

	/**
	 * Load settings from predefined file.
	 */
	public static void loadProperties(){
		
		settings = new Properties();
		FileInputStream in;
		
		try {
			in = new FileInputStream(SETTINGS_FILE_NAME);
			settings.load(in);
			in.close();
			
			tagsActive = new boolean [getIntValue("TAG_COUNT")];
			setTagsActive(getStringValue("TAGS_ACTIVE"));
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * Returns key's value as Integer.
	 * 
	 * @param key
	 * @return
	 */
	public static int getIntValue(String key) {
		return Integer.parseInt(settings.getProperty(key));
	}
	
	/**
	 * Returns key's value as Long.
	 * @param key
	 * @return
	 */
	public static long getLongValue(String key){
		return Long.parseLong (settings.getProperty (key));
	}
	
	/**
	 * Returns key's value as String.
	 * @param key
	 * @return
	 */
	public static String getStringValue(String key){
		return settings.getProperty (key);
	}
	
	/**
	 * Returns key's value as Double.
	 * @param key
	 * @return
	 */
	public static double getDoubleValue(String key){
		return Double.parseDouble (settings.getProperty (key));
	}
	
	/**
	 * Returns key's value as Double.
	 * @param key
	 * @return
	 */
	public static boolean getBoolValue(String key){
		return settings.getProperty(key).toLowerCase().equals("true");
	}

	public static Object[] getObjectsOfObjectsOfIntTable(String key) {
		String data = settings.getProperty(key) + "";
		LinkedList<String> allPeriodsData = new LinkedList<String>();
		while (data.contains(";")) {
			allPeriodsData.addLast(data.substring(0, data.indexOf(";")));
			data = data.substring(data.indexOf(";") + 1) + "";
		}
		allPeriodsData.addLast(data);

		LinkedList<LinkedList<String>> dataPerPeriods = new LinkedList<LinkedList<String>>();
		for (int i = 0; i < allPeriodsData.size(); i++) {
			String onePeriodData = allPeriodsData.get(i) + "";
			LinkedList<String> onePeriodIntervals = new LinkedList<String>();
			while (onePeriodData.contains(":")) {
				onePeriodIntervals.addLast(onePeriodData.substring(0,
						onePeriodData.indexOf(":")));
				onePeriodData = onePeriodData.substring(onePeriodData
						.indexOf(":") + 1)
						+ "";
			}
			onePeriodIntervals.addLast(onePeriodData);
			dataPerPeriods.addLast(onePeriodIntervals);
		}

		Object[] returnObj = new Object[dataPerPeriods.size()];
		for (int i = 0; i < dataPerPeriods.size(); i++) {
			Object[] tempObj = new Object[dataPerPeriods.get(i).size()];
			for (int j = 0; j < dataPerPeriods.get(i).size(); j++) {
				int[] tmpInt = new int[2];
				String onePeriodOneLevel = dataPerPeriods.get(i).get(j) + "";
				tmpInt[0] = Integer.parseInt(onePeriodOneLevel.substring(0,
						onePeriodOneLevel.indexOf(",")));
				tmpInt[1] = Integer.parseInt(onePeriodOneLevel
						.substring(onePeriodOneLevel.indexOf(",") + 1));
				tempObj[j] = tmpInt;
			}
			returnObj[i] = tempObj;
		}
		return returnObj;
	}

	/**
	 * Returns a table of Strings, separated with comma. String:
	 * <pre>
	 * s1, s2, s3, ...
	 * </pre>
	 * is returned as:
	 * <pre>
	 * [s1, s2, s3, ...]
	 * </pre>
	 * @param key
	 * @return
	 */
	public static String[] getStringTable(String key) {
		return settings.getProperty(key).split(",\\s*");
	}
	
	
	/**
	 * Returns a table of Integers, separated with comma. String:
	 * <pre>
	 * 1, 2, 3, ...
	 * </pre>
	 * is returned as:
	 * <pre>
	 * [1, 2, 3, ...]
	 * </pre>
	 * @param key
	 * @return
	 */
	public static int[] getIntTable(String key) {
		String dataList[] = settings.getProperty(key).split(",\\s*");
		int[] ret = new int[dataList.length];
		for (int i = 0; i < dataList.length; i++)
			ret[i] = Integer.parseInt(dataList[i]);

		return ret;
	}
	
	/**
	 * Returns two-dimensional table of Doubles. The key must be in the following format:
	 * <pre>
	 * x1, x2, x3; x4, x5, x6; ...
	 * </pre>
	 * which is parsed as:
	 * <pre>
	 * [[x1, x2, x3], [x4, x5, x6], ...]
	 * </pre>
	 * @param key
	 * @return
	 */
	public static double[][] getDoubleTableTable(String key){
		
		double result[][];
		
		String data = settings.getProperty(key);
		String lines[] = data.split(";\\s*");
		String cols[] = lines[0].split(",\\s*");
		
		result = new double[lines.length][cols.length];
		
		for(int i = 0; i < result.length; i++){
			cols = lines[i].split(",\\s*");
			for(int j = 0; j < cols.length; j++)
				result[i][j] = Double.parseDouble(cols[j]);
		}
		
		return result;
	}
	
	/**
	 * Set active tags.
	 * 
	 * @param pTagsActive active tags as a string of 0 and 1
	 */
	public static void setTagsActive (String pTagsActive) {
		for (int i = 0; i < getIntValue("TAG_COUNT"); i++) {
			if ((i < pTagsActive.length ()) && (pTagsActive.charAt (i) == '1')) {
				tagsActive [i] = true;
			} else {
				tagsActive [i] = false;
			}
		}
	}
	

	/**
	 * Returns the status of the chosen tag.
	 * 
	 * @param index index of the tag
	 * @return <code>true</code> if the chosen tag is active and <code>false</code> if it is inactive
	 */
	public static boolean getTagStatus (int index) {
		return tagsActive [index];
	}
	
	
	
	/**
	 * Sets the status of a tag.
	 * 
	 * @param index tag index for status change
	 * @param status if <code>true</code> makes the tag active, if <code>false</code> makes the tag inactive
	 */
	public static void setTagStatus (int index, boolean status) {
		tagsActive [index] = status;
	}
	
	/**
	 * Returns all tag statuses.
	 * 
	 * @return all tag statuses
	 */
	public static boolean [] getStatus () {
		return tagsActive;
	}
	
	/**
	 * Counts number of active tags.
	 * 
	 * @return numbe of tags
	 */
	public static int countActiveTags () {
		int count = 0;
		
		for(int i = 0; i < tagsActive.length; i++)
			if(tagsActive[i])	count++;
		
		return count;
	}
	
	
}